/**
 * 获取时间
 * 向前/后推/一天一周/一月/一年
 * 时间格式化
 */
var getTime = {
  getNowTime: function () {
    var date = new Date();
    var nowDate = this.formatDate(date, "YYYY-MM-DD HH:NN:SS");
    return nowDate;
  },

  getNowDate: function () {
    var date = new Date();
    var nowDate = this.formatDate(date, "YYYY-MM-DD");
    return nowDate;
  },
  getNowYear: function () {
    var date = new Date();
    var nowDate = this.formatDate(date, "YYYY");
    return nowDate;
  },
  getNowMouth: function () {
    var date = new Date();
    var nowDate = this.formatDate(date, "YYYY-MM");
    return nowDate;
  },

  getNowHour: function () {
    var date = new Date();
    var nowDate = this.formatDate(date, "YYYY-MM-DD HH");
    return nowDate;
  },
  diffTimes: function (date) {
    var nowDate = this.formatDate(date, "HH:NN:SS");
    return nowDate;
  },
  diffDayTimes: function (date, diffType) {
    var nowDate = this.formatDate(date, diffType);
    return nowDate;
  },
  //月
  difftimebymonth: function (cTime, diffmonths) {
    var date = new Date(Date.parse(cTime));
    var numMonths = Number(diffmonths);
    date.setMonth(date.getMonth() + numMonths);
    return this.formatDate(date, "YYYY-MM-DD HH:NN:SS");
  },
  //天
  difftimebyday: function (cTime, diffdays) {
    var date = new Date(Date.parse(cTime));
    var numDays = Number(diffdays);
    date.setDate(date.getDate() + numDays);
    // return date.Format("yyyy-MM-dd HH:mm:ss");
    return this.formatDate(date, "YYYY-MM-DD HH:NN:SS");
  },
  //小时
  difftimebyhour: function (cTime, diffhours) {
    var date = new Date(Date.parse(cTime));
    var numHours = Number(diffhours);
    date.setHours(date.getHours() + numHours);
    return this.formatDate(date, "YYYY-MM-DD HH:NN:SS");
  },
  //分钟
  difftimebymins: function (cTime, diffmins) {
    var dt = new Date(Date.parse(cTime));
    var numMins = Number(diffmins);
    dt.setMinutes(dt.getMinutes() + numMins);
    return this.formatDate(dt, "YYYY-MM-DD HH:NN:SS");
  },
  //秒
  difftimebysecond: function (cTime, diffseconds) {
    var date = new Date(Date.parse(cTime));
    var numHours = Number(diffseconds);
    date.setSeconds(date.getSeconds() + numHours);
    return this.formatDate(date, "YYYY-MM-DD HH:NN:SS");
  },

  getDiffSeconds: function (t1, t2) {
    var stime = new Date(Date.parse(t1));
    var etime = new Date(Date.parse(t2));
    var diff_millisecond = stime - etime;
    return diff_millisecond / 1000;
  },
  getCurrWeek: function (z) {
    let str = "星期";
    let week = new Date().getDay();
    switch (week) {
      case 0:
        str += "日";
        break;
      case 1:
        str += "一";
        break;
      case 2:
        str += "二";
        break;
      case 3:
        str += "三";
        break;
      case 4:
        str += "四";
        break;
      case 5:
        str += "五";
        break;
      case 6:
        str += "六";
        break;
    }
    return str;
  },
  formatDate: function (date, type) {
    if (
      (date.getTime || new Date(date).getTime()) &&
      typeof type === "string"
    ) {
      date = type.replace(/YYYY|MM|DD|HH|hh|NN|nn|SS|ss/g, function ($) {
        switch ($) {
          case "YYYY":
            return date.getFullYear();
          case "MM":
            return date.getMonth() >= 9
              ? date.getMonth() + 1
              : "0" + (date.getMonth() + 1);
          case "mm":
            return date.getMonth() + 1;
          case "DD":
            return date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
          case "dd":
            return date.getDate();
          case "HH":
            return date.getHours() > 9
              ? date.getHours()
              : "0" + date.getHours();
          case "hh":
            return date.getHours();
          case "NN":
            return date.getMinutes() > 9
              ? date.getMinutes()
              : "0" + date.getMinutes();
          case "nn":
            return date.getMinutes();
          case "SS":
            return date.getSeconds() > 9
              ? date.getSeconds()
              : "0" + date.getSeconds();
          case "ss":
            return date.getSeconds();
          default:
            return $;
        }
      });
    }
    return date;
  },
};
export default getTime;
